/*
 * Mostra uma noticia
 * */
package servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.Categoria;
import beans.Noticia;
import dbclass.CategoriaDAO;
import dbclass.CategoriaJDBC;
import dbclass.NoticiaDAO;
import dbclass.NoticiaJDBC;
import dbclass.VotoDAO;
import dbclass.VotoJDBC;

/**
 * Servlet implementation class NoticiasVer
 */
public class NoticiasVer extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NoticiasVer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		Noticia noticia = null;
		NoticiaDAO noticiaDAO = new NoticiaJDBC();
		VotoDAO votoDAO = new VotoJDBC();
		Long nota = null; 
			
		String id = (String)request.getParameter("id"); 
		 
		try {  
			 noticia = noticiaDAO.findById((long)Integer.parseInt(id)); 
			 nota = votoDAO.avg((long)Integer.parseInt(id));

		} catch (SQLException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();   
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		}
		session.setAttribute("noticia",noticia);  
		session.setAttribute("nota",Math.round(nota));   
		
		String url="/NoticiaVer.jsp";   
		ServletContext sc = getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher(url);
		rd.forward(request, response); 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
